package com.gangan.controller.web;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gangan.controller.common.BaseController;
import com.gangan.model.Ad;
import com.gangan.model.AdCate;
import com.gangan.model.CmsCategory;
import com.gangan.model.CmsContent;
import com.gangan.model.view.CmsCategoryTreeView;
import com.gangan.model.view.CmsCategoryView;
import com.gangan.service.IAdCateService;
import com.gangan.service.IAdService;
import com.gangan.service.ICmsCategoryService;
import com.gangan.service.ICmsContentService;
import com.gangan.tools.LogicException;
import com.gangan.tools.MessageBean;
import com.gangan.tools.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengweicheng
 * @since 2019-09-04
 */
@Api(description = "*内容分类模块控制层")
@RestController
@RequestMapping("/cmsCateory")
public class CmsCateoryController extends BaseController {

    @Autowired
    ICmsCategoryService cmsCategoryService;
    @Autowired
    ICmsContentService cmsContentService;

   @RequestMapping(value = "/findCateById",method = RequestMethod.GET)
    @ApiOperation(value="内容模块相关数据", notes="根据id查询该id本身数据以及底下子数据")
    public MessageBean findCateById(Integer id){
        //根据id查询出该id下的所有子分类与自身
        List<CmsCategory> cmsCategories = cmsCategoryService.select(Wrappers.<CmsCategory>query().lambda().eq(CmsCategory::getId,id).or(wrapper ->wrapper.eq(CmsCategory::getParentId,id)));
        if(CollectionUtils.isEmpty(cmsCategories)){
            throw new LogicException("请输入正确有效的内容分类id");
        }
        //过滤掉子分类
        CmsCategory cms = cmsCategories.stream().filter(cmsCategory->cmsCategory.getId().equals(id)).findFirst().get();
        //设置子分类
        cms.setChildrens(cmsCategories.stream().filter(cmsCategory -> !cmsCategory.getId().equals(id)).collect(Collectors.toList()));
        return resultSuccess(cms);
    }
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    @ApiOperation(value="内容分类树状图", notes="根据相关条件查询列表数据")
    public MessageBean tree(){
        List<CmsCategoryTreeView> views= cmsCategoryService.list().stream().map(CmsCategoryTreeView::new).collect(Collectors.toList());
        return resultSuccess(TreeUtil.buildByLoop(views,0));
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value="内容分类列表", notes="根据相关条件查询列表数据")
    public MessageBean list(Page page){
        return resultSuccess(cmsCategoryService.page(page,Wrappers.<CmsCategory>query().lambda().orderByAsc(CmsCategory::getListsorder)));
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ApiOperation(value="移除内容分类", notes="根据自增主键移除数据")
    public MessageBean del(Integer id){
        if(CollectionUtils.isNotEmpty(cmsContentService.list(Wrappers.<CmsContent>query().lambda().eq(CmsContent::getCateId,id)))){
            return resultFailed("该分类下有内容数据,无法删除.");
        }else if(CollectionUtils.isNotEmpty(cmsCategoryService.list(Wrappers.<CmsCategory>query().lambda().eq(CmsCategory::getParentId,id)))){
            return resultFailed("该分类下有子内容分类,无法删除.");
        }
        return resultSuccess(cmsCategoryService.removeById(id));
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value="新增内容分类", notes="将提交的数据保存到数据库")
    public MessageBean add(CmsCategory add){
        return resultSuccess(cmsCategoryService.save(add));
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value="编辑内容分类", notes="根据主键修改数据")
    public MessageBean edit(CmsCategory edit){
        return resultSuccess(cmsCategoryService.updateById(edit));
    }
}
